﻿@{
    ViewBag.Title = "一沙后台管理系统 - 登录";
    Layout = "~/Views/Shared/_Layout.cshtml";
    var year = DateTime.Now.Year;
}
@inject Microsoft.AspNetCore.Hosting.IWebHostEnvironment HostingEnvironment

<link href='@Url.Content("~/css/ruoyi-login.css")' rel="stylesheet" />

<style>
    .hidden {
        display: none !important
    }
</style>
<div class="signin">
    <div class="signinpanel">
        <div class="row">
            <div class="col-sm-7">
                <div class="signin-info">
                    <div class="logopanel m-b">
                        <h1><img alt="[ 若依 ]" src="~/img/ruoyi.png"></h1>
                    </div>
                    <div class="m-b"></div>
                    <h4>欢迎使用 <strong>若依 后台管理系统</strong></h4>
                    <ul class="m-b">
                        <li><i class="fa fa-arrow-circle-o-right m-r-xs"></i> .NET5</li>
                        <li><i class="fa fa-arrow-circle-o-right m-r-xs"></i> EF、SqlSugar</li>
                    </ul>
                    @*<strong th:if="${@config.getKey('sys.account.registerUser')}">还没有账号？ <a th:href="@{/register}">立即注册&raquo;</a></strong>*@
                </div>
            </div>
            <div class="col-sm-5">
                <form id="form">
                    <h4 class="no-margin">登录：</h4>
                    <p class="m-t-md">一沙一世界，一叶一菩提</p>

                    <p>用户名：admin&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;密码：123456</p>

                    <div class="">
                        <input id="userName" name="userName" class="form-control uname" name="UserName" type="text" placeholder="用户名" value="@ViewBag.UserName" />
                    </div>
                    <div class="">
                        <input id="password" name="password" class="form-control pword m-b" name="Password" type="password" placeholder="密码" value="@ViewBag.Password" />
                    </div>
                    <div class="row @(ViewBag.ErrorNum >= 3 ? "" : "hidden")" id="divcode">
                        <div class="col-xs-6">
                            <input id="captchaCode" name="captchaCode" type="text" name="CaptchaCode" class="form-control code" placeholder="验证码" maxlength="3">
                        </div>
                        <div class="col-xs-6">
                            <a href="javascript:void(0);" title="点击更换验证码">
                                <img id="imgCaptchaCode" src='@Url.Content("~/Home/GetCaptchaImage")' height="34" />
                            </a>
                        </div>
                    </div>
                    <div class="checkbox-custom">
                        <input id="rememberMe" name="rememberMe" type="checkbox"> <label for="rememberMe">记住我</label>
                    </div>
                    <button class="btn btn-success btn-block" onclick="loginForm(); return false;">登录</button>
                </form>
            </div>
        </div>
        <div class="signup-footer">
            <div class="pull-left">
                Copyright © 2021-@year ruoyi.vip All Rights Reserved. <br>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(function () {
        if ($.cookie('RememberMe') == 1) {
            $("#rememberMe").prop("checked", true);
            if ("@GlobalContext.SystemConfig.LoginProvider" == "Cookie") {
                if (!ys.isNullOrEmpty($.cookie('UserToken'))) {
                    redirectToIndex();
                }
            }
            else {
                $.removeCookie("UserToken", { path: '/' });
            }
        }
        $("#rememberMe").click(function () {
            if ($(this).prop("checked")) {
                $.cookie('RememberMe', 1, { expires: 30, path: '/' });
            }
        });

        $("#imgCaptchaCode").click(function () {
            $(this).attr("src", '/Home/GetCaptchaImage' + '?t=' + new Date().getTime());
        });

        $("#form").validate({
            rules: {
                userName: { required: true },
                password: { required: true }
            }
        });

        $.validator.setDefaults({
            submitHandler: function () {
                loginForm();
            }
        });
    });

    $(document).keydown(function (event) {
        if (event.keyCode == 13) {
            loginForm();
        }
    });
    //用户登录
    function loginForm() {
        if ($("#form").validate().form()) {
            var postData = $("#form").formSerialize();
            ys.ajax({
                url: '/Home/DoLogin',
                type: "post",
                data: postData,
                success: function (obj) {
                    if (obj.code == 1000) {
                        ys.msgSuccess(obj.msg);
                        setTimeout(function () {
                            redirectToIndex();
                        }, 900);
                    }
                    else {
                        if (obj.code >= 3) {
                            $('#divcode').removeClass('hidden')
                        }
                        ys.msgError(obj.msg);
                    }
                }
            });
        }
    }

    function redirectToIndex() {
        top.document.location.href = '/Home/Index';
        //移除菜单缓存
        //storage.remove('publicPath');
        //刷新父页面
        //top.location.reload();
        // self.opener.location.reload();
    }
</script>
